package com.woniuxueyuan.Controller;

import com.nimbusds.jose.JOSEException;
import com.woniuxueyuan.entity.User;
import com.woniuxueyuan.entity.dto.AddrAndUserCartDTO;
import com.woniuxueyuan.entity.dto.LoginDTO;
import com.woniuxueyuan.service.UserService;
import com.woniuxueyuan.util.ResponseData;
import com.woniuxueyuan.util.ResponseEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @PostMapping("/login")
    public ResponseData login(@RequestBody User user){
        try {
           LoginDTO loginDTO =  userService.login(user);
           if(loginDTO !=null){
               return ResponseData.ok(loginDTO);
           }else {
               return ResponseData.fail(ResponseEnum.LOGINERROR);
           }

        } catch (JOSEException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }

    }
    @GetMapping("/addCart/{id}/{buyNum}")
    public ResponseData addCart(@PathVariable("id") Integer id,
                                @PathVariable("buyNum") Integer buyNum){
        try {
            userService.addCart(id,buyNum);
            return ResponseData.ok();
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

    /**
     * 查询地址
     * 查询购物车信息
     * @return
     */
    @GetMapping("/queryAddrAndUserCart")
    public ResponseData queryAddrAndUserCart (){
        try {
          Map map= userService.queryAddrAndUserCart();
            return ResponseData.ok(map);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
